<!-- 项目编辑弹窗 -->
<style>
.unitSearch{
  display: inline-block;
  white-space: nowrap;
  margin-right: -5px;
  .el-input{
    width:180px !important;
  }
  .el-input__inner{
    width:180px !important;
  }
  .el-input--suffix .el-input__inner{
    width:180px !important;
  }
  .el-input__icon{
    height:100%;
  }
  .ele-fluid {
    width: auto !important;
  }
}
</style>
<template>
  <el-dialog
    :title="isUpdate?'修改':'添加'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :close-on-click-modal="false"
    :before-close="confirmClose"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item label="app名称:" prop="app_id">
        <el-select
          clearable
          v-model="form.app_id"
          placeholder="请选择应用名称"
          class="ele-fluid"
        >
          <el-option v-for="item in appNames"  :key="item.id" :label="item.app_name" :value="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="应用类型" prop="type">
        <el-select
          clearable
          v-model="form.type"
          placeholder="请选择应用类型"
          class="ele-fluid"
        >
          <el-option v-for="(value,idx) in appTypeToStr"  :key="idx" :label="value" :value="parseInt(idx)"/>
        </el-select>
      </el-form-item>
      <el-form-item
        label="最新版本:"
        prop="version">
        <el-input
          :maxlength="20"
          v-model="form.version"
          placeholder="请输入最新版本"
          clearable/>
      </el-form-item>

      <el-form-item
        label="最新版本号:"
        prop="code">
        <el-input
          :maxlength="20"
          v-model="form.code"
          placeholder="请输入最新版本号"
          type="number"
          clearable/>
      </el-form-item>

      <el-form-item  label="指定版本更新范围" prop="range" label-width="140px">
        <div class="unitSearch">
          <el-select
            clearable
            v-model="form.range"
            placeholder="请选择版本更新范围"
            class="ele-fluid"
          >
            <el-option  v-for="(value,idx) in rangeToStr"  :key="idx" :label="value" :value="parseInt(idx)"/>
          </el-select>
          <el-input
            :maxlength="20"
            v-model="form.range_version"
            placeholder="请输入要更新的版本号"
            type="text"
            clearable/>
        </div>
      </el-form-item>
      <el-form-item
        label="更新地址:"
        prop="url">
        <el-input
          v-model="form.url"
          placeholder="请输入更新地址"
          clearable/>
      </el-form-item>


      <el-form-item label="更新方式" prop="update_type">
        <el-select
          clearable
          v-model="form.update_type"
          placeholder="请选择更新方式"
          class="ele-fluid"
        >
          <el-option v-for="(value,idx) in updateTypeToStr"  :key="idx" :label="value" :value="parseInt(idx)"/>
        </el-select>
      </el-form-item>

      <el-form-item label="是否开启" prop="status">
        <el-select
          clearable
          v-model="form.status"
          placeholder=""
          class="ele-fluid"
        >
          <el-option v-for="(value,idx) in statusArr"  :key="idx" :label="value" :value="parseInt(idx)"/>
        </el-select>
      </el-form-item>

      <el-form-item label="更新内容:">
        <tinymce-editor v-model="form.content" :init="initEditor()"/>
      </el-form-item>

      <el-form-item
        label="更新时间:"
        prop="update_date">
        <el-date-picker
          v-model="form.update_date"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>

      </el-form-item>

      <el-form-item
        label="更新备注:"
        prop="desc">
        <el-input
          :maxlength="20"
          v-model="form.desc"
          placeholder="请输入更新备注"
          clearable/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import TinymceEditor from '@/components/TinymceEditor';

export default {
  name: 'BoxAppVersionEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    appNames : Array,
    appTypeToStr: [],
    rangeToStr: [],
    updateTypeToStr: [],
    statusArr: {}
  },
  components: {TinymceEditor},
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        app_id: [
          {required: true, message: '请选择应用名称', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入应用类型  1安卓  2 ios', trigger: 'blur'}
        ],
        range: [
          {required: true, message: '请选择', trigger: 'blur'}
        ],
        range_version: [
          {required: true, message: '请输入范围版本', trigger: 'blur'}
        ],
        version: [
          {required: true, message: '请输入最新版本', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入最新版本号', trigger: 'blur'}
        ],
        update_type: [
          {required: true, message: '请输入 1 热更新  2弱更新  3强更', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请输入状态  1 有限  2无效', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '请输入更新地址', trigger: 'blur'}
        ],
        update_date: [
          {required: true, message: '请输入更新时间', trigger: 'blur'}
        ],
        desc: [
          {required: true, message: '请输入更新备注', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入更新内容', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/boxAppVersion/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          if(!this.isUpdate){
            this.form = {};
          }
        })
        .catch(() => {});
    },
    // 初始化富文本
    initEditor() {
      return {
        height: 300,
        branding: false,
        skin_url: '/tinymce/skins/ui/oxide',
        content_css: '/tinymce/skins/content/default/content.css',
        language_url: '/tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
        toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
        toolbar_drawer: 'sliding',
        images_upload_handler: (blobInfo, success, error) => {
          let file = blobInfo.blob();
          // 使用axios上传
          const formData = new FormData();
          formData.append('file', file, file.name);
          this.$http.post('/upload/uploadImage', formData).then(res => {
            if (res.data.code == 0) {
              success(res.data.data);
            } else {
              error(res.data.msg);
            }
          }).catch(e => {
            error(e.message);
          });
        },
        file_picker_types: 'media',
        file_picker_callback: () => {
        }
      }
    },
  }
}
</script>

<style scoped>
</style>
